<?php
require_once ("BaseDAO.php");

// Address Value Object
class Address extends BaseVO {
	var $id;
	var $line1;
	var $line2;
	var $city;
	var $state;
	var $zip;

	// create a new VO
	function Address(
		$id= 0,
		$line1= "",
		$line2= "",
		$city= "",
		$state= "",
		$zip= ""
	) {
		$this->id = $id;
		$this->line1 = $line1;
		$this->line2 = $line2;
		$this->city = $city;
		$this->state = $state;
		$this->zip = $zip;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->line1 == $vo->line1 &&
			$this->line2 == $vo->line2 &&
			$this->city == $vo->city &&
			$this->state == $vo->state &&
			$this->zip == $vo->zip;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->line1 = $vo->line1;
		$this->line2 = $vo->line2;
		$this->city = $vo->city;
		$this->state = $vo->state;
		$this->zip = $vo->zip;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->line1 . "," . $this->line2 . "," . $this->city . "," . $this->state . "," . $this->zip;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<line1>$this->line1</line1>\n" .
		"<line2>$this->line2</line2>\n" .
		"<city>$this->city</city>\n" .
		"<state>$this->state</state>\n" .
		"<zip>$this->zip</zip>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->line1 = $this->formHelper($prefix . "line1", "");
		$this->line2 = $this->formHelper($prefix . "line2", "");
		$this->city = $this->formHelper($prefix . "city", "");
		$this->state = $this->formHelper($prefix . "state", "");
		$this->zip = $this->formHelper($prefix . "zip", "");
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->line1 = $this->queryHelper($prefix . "line1", "");
		$this->line2 = $this->queryHelper($prefix . "line2", "");
		$this->city = $this->queryHelper($prefix . "city", "");
		$this->state = $this->queryHelper($prefix . "state", "");
		$this->zip = $this->queryHelper($prefix . "zip", "");
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// Address Data Access Object
class AddressDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `address` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `address` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `address` (line1,line2,city,state,zip) VALUES ('%A','%B','%C','%D','%E')";
	var $SQL_UPDATE = "UPDATE `address` SET ";
	var $SQL_DELETE = "DELETE FROM `address` WHERE id=%A";

	// default constructor
	function AddressDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single Address obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new Address(
				$row->id, 
				$row->line1, 
				$row->line2, 
				$row->city, 
				$row->state, 
				$row->zip
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of Address objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Address(
				$row->id, 
				$row->line1, 
				$row->line2, 
				$row->city, 
				$row->state, 
				$row->zip
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Address objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Address(
				$row->id, 
				$row->line1, 
				$row->line2, 
				$row->city, 
				$row->state, 
				$row->zip
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Address objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->line1) > 0) $tmp .= "line1 like '%" . $this->safeEscapeStr($example->line1) . "%' and ";
		if(strlen($example->line2) > 0) $tmp .= "line2 like '%" . $this->safeEscapeStr($example->line2) . "%' and ";
		if(strlen($example->city) > 0) $tmp .= "city like '%" . $this->safeEscapeStr($example->city) . "%' and ";
		if(strlen($example->state) > 0) $tmp .= "state like '%" . $this->safeEscapeStr($example->state) . "%' and ";
		if(strlen($example->zip) > 0) $tmp .= "zip like '%" . $this->safeEscapeStr($example->zip) . "%' and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Address(
				$row->id, 
				$row->line1, 
				$row->line2, 
				$row->city, 
				$row->state, 
				$row->zip
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->line1), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->line2), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->city), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->state), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->zip), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "line1 = '" . $this->safeEscapeStr($vo->line1) . "', ";
		$this->sql .= "line2 = '" . $this->safeEscapeStr($vo->line2) . "', ";
		$this->sql .= "city = '" . $this->safeEscapeStr($vo->city) . "', ";
		$this->sql .= "state = '" . $this->safeEscapeStr($vo->state) . "', ";
		$this->sql .= "zip = '" . $this->safeEscapeStr($vo->zip) . "' ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

